//request.js文件

import jwtDecode from "jwt-decode";
import loginHomeVue from "@/pages/login/login-home.vue";
import xcxInfo from '@/common/manager/xcxInfo.js';



let server_url = xcxInfo.domainName

let successCode = 20000
//加密手机号临牌===================================
import JSEncrypt from 'jsencrypt';
import { key } from '@/pages/login/publicKey.js';
let timer = null
let codekey = null
export let encryptionFun = () => {
	timer = String(new Date().getTime());
	let jse = new JSEncrypt();
	jse.setPublicKey(key);
	codekey = jse.encrypt(timer.toString()); // 进行加密
}
encryptionFun()
// ==============================================

export default class Request {
	http(param) {
		// 请求参数
		let url = param.url;
		let method = param.method;
		let header = param.header || {};
		let data = Object.assign(param.data || {});
		let hideLoading = param.hideLoading || false;
		let whetherData = param.whetherData;
		//拼接完整请求地址
		let requestUrl = server_url + url;
		//请求方式:GET或POST(POST需配置
		if (method) {
			let token = uni.getStorageSync("token")

			method = method.toUpperCase(); //小写改为大写
			// 根据请求方法设置 content-type
			let contentType = method === "POST" ? "application/x-www-form-urlencoded" : "application/json";
			// 合并 header
			header = Object.assign({
				contentType,
				Authentication: codekey,
				Timer:timer
			}, param.header, {
				token: token
			})

			// if (method == "POST") {
			// 	header = Object.assign({
			// 		'content-type': "application/x-www-form-urlencoded"
			// 	}, param.header, {
			// 		token: token
			// 	})
			// } else {
			// 	header = Object.assign({
			// 		'content-type': "application/json",
			// 		"Authentication": codekey,
			// 		"Timer":timer
			// 	}, param.header, {
			// 		token: token
			// 	})
			// }





		}
		//加载圈
		// if (hideLoading) {
		// 	uni.showLoading({
		// 		title: "加载中..."
		// 	})
		// }


		// 返回promise
		return new Promise((resolve, reject) => {
			// 请求
			uni.request({
				url: requestUrl,
				data: data,
				method: method,
				header: header,
				timeout: 15000,
				success: (res) => {

					if (hideLoading) {
						// uni.hideLoading();
					}

					// 判断网络请求code
					if (res.statusCode == 200) {
						// 判断服务器code
						if (whetherData) {
							if (res.data.code == successCode) {
								// 成功   



								const newToken = res.header.token
								const oldToken = uni.getStorageInfoSync("token")
								if (newToken && newToken != oldToken) {
									uni.setStorageSync("token", newToken)
									const userInfo = jwtDecode(newToken).data
									getApp().globalData.userInfo = userInfo
								}

								resolve(res.data)
								return true;



							} else {
								// 失败
								if (res.data.code == 40305) {
									uni.removeStorageSync("token")
									getApp().globalData.userInfo = undefined
									uni.navigateTo({
										url: '/pages/login/login'
									})
								} else if (res.data.code == 40307) {
									reject(res.data)
									if (res.data.msg.length > 0) {
										uni.showToast({
											title: res.data.msg,
											icon: 'none'
										})
									}
								} else if (res.data.code == 44444) {

									resolve(res.data)
									return true;
								} else {
									uni.showToast({
										title: res.data.msg,
										icon: 'none'
									})
									reject(res.data)
								}

								return false;
							}
						} else {
							resolve(res.data)
							return true;
						}
					} else {
						reject(res.data)
						return false;
					}
				},
				//请求失败
				fail: (err) => {
					if (hideLoading) {
						uni.hideLoading();
					}
					uni.showToast({
						title: '获取数据失败，请重试',
						icon: 'none'
					})
					resolve(err)
					return false;
				},
				//请求完成
				complete() {
					return;
				}
			})
		})
	}
}